package commandExecution;

import java.io.*;
import java.util.ArrayList;

public class CommandRunner {
	
	private static ArrayList<String> output = new ArrayList<String>();
	
	//private static ArrayList<Integer> output_size = new ArrayList<Integer>();
	private static String output_size;

    public static void runFind(String path, String fileTypes[]) {
    	
    	String[] command = {"/bin/sh", "-c", CommandGenerator.generateFind(path, fileTypes)};
    	
    	

        String s = null;
        
       // ArrayList<String> output = new ArrayList<String>();

        try {
            Process p = Runtime.getRuntime().exec(command);
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
                output.add(s);
            }
            
            // read any errors from the attempted command
            System.out.println("\n\nHere is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            
        }
        catch (IOException e) {
            System.out.println("\n\nexception happened - here's what I know: ");
            e.printStackTrace();
        }
        
        //return output;
        
    }
    
    public static void runCopy(String[] originPaths, String targetPath) {
    	
    	String[] command = {"/bin/sh", "-c", CommandGenerator.generateCopy(originPaths, targetPath)};
    	
    	String s = null;

        try {
            Process p = Runtime.getRuntime().exec(command);
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            
            // read any errors from the attempted command
            System.out.println("\n\nHere is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            
        }
        catch (IOException e) {
            System.out.println("\n\nexception happened - here's what I know: ");
            e.printStackTrace();
        }
    	
    }
    
    public static void run_mkdir(String targetPath, String dirName) {

		String[] command = {"/bin/sh", "-c", CommandGenerator.generate_mkdir(targetPath, dirName)};

		String s = null;

		try {
			Process p = Runtime.getRuntime().exec(command);

			BufferedReader stdInput = new BufferedReader(new 
					InputStreamReader(p.getInputStream()));

			BufferedReader stdError = new BufferedReader(new 
					InputStreamReader(p.getErrorStream()));

			// read the output from the command
			System.out.println("\n\nHere is the standard output of the command:\n");
			while ((s = stdInput.readLine()) != null) {
				System.out.println(s);
			}

			// read any errors from the attempted command
			System.out.println("\n\nHere is the standard error of the command (if any):\n");
			while ((s = stdError.readLine()) != null) {
				System.out.println(s);
				if(s.contains("No such file or directory")) {
					String newName = s.substring(s.lastIndexOf("/") + 1, s.lastIndexOf(":"));
					String newPath = s.substring(s.indexOf("/"), s.lastIndexOf("/") + 1);
					System.out.println("\n\n");
					run_mkdir(newPath, newName);
					run_mkdir(targetPath, dirName);
				}
			}

		}
		catch (IOException e) {
			System.out.println("\n\nexception happened - here's what I know: ");
			e.printStackTrace();
		}
    }

    
  public void runSize() {
    	
	  String[] command_size = {"/bin/sh", "-c","df","/"};
    	
	  String s = null;
        
       // ArrayList<String> output = new ArrayList<String>();

        try {
            Process p = Runtime.getRuntime().exec(command_size);
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
               System.out.println(s);
            	//System.out.println(s.split(" ")[8]);
                //output_size.add(Integer.parseInt(s.split(" ")[3]));
                output_size = s.split(" ")[8];
            }
            
            // read any errors from the attempted command
            System.out.println("\n\nHere is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            
        }
        catch (IOException e) {
            System.out.println("\n\nexception happened - here's what I know: ");
            e.printStackTrace();
        }
        
        //return output;
        
    }
  
  
  
  	public static int getOutputSize(){
  		
  		return Integer.parseInt(output_size);
  	} 
    
    public static ArrayList<String> getOutput(){
    	return output;
    } 
}